import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { ApiProvider } from '../../providers/api/api'
import { Md5 } from 'ts-md5/dist/md5';
import { Storage } from '@ionic/storage';

/**
 * Generated class for the UserAgentDetailPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
    selector: 'page-user-agent-detail',
    templateUrl: 'user-agent-detail.html',
})
export class UserAgentDetailPage {
    public index: Number;
    public chooseArray: any = ['one','two'];
    public choose: String;
    public uid: any;
    public resData: any = {};
    public Arr: Array<any>;
    public downlone: any = {};
    public data: any = {}

    constructor(
        public navCtrl: NavController, 
        public navParams: NavParams,
        public Api: ApiProvider,
        public storage: Storage ) {

        storage.get('userId').then(
            (value: string) => {
                this.uid = value
            }
        );

        this.index = navParams.get('index');

        
    }

    ionViewDidEnter() {
        console.log('ionViewDidEnter UserAgentDetailPage');
        switch (this.index) {
            case 1:
                this.choose = this.chooseArray[0];
                this.PostOne();
                break;

            case 2:
                this.choose = this.chooseArray[1];
                this.PostTwo();
                break;
        }

    }

    PostOne(){
        let timeStamp = (new Date()).getTime();
        if( this.uid ) {
            this.Api.oneLine({
                times: timeStamp,
                token: Md5.hashStr( 'HalfPriceMall' + timeStamp ),
                userid: this.uid
            }).then(res=>{
                this.resData = res;
                if( this.resData.code == 1 ) {
                    this.data = this.resData.data;
                    this.Arr = [];
                    this.Arr = this.data.list;
                    this.downlone = this.data.thesum;
                }
            })
        }
    };

    PostTwo(){
        let timeStamp = (new Date()).getTime();
        if( this.uid ) {
            this.Api.twoLine({
                times: timeStamp,
                token: Md5.hashStr( 'HalfPriceMall' + timeStamp ),
                userid: this.uid
            }).then(res=>{
                this.resData = res;
                if( this.resData.code == 1 ) {
                    this.data = this.resData.data;
                    this.Arr = [];
                    this.Arr = this.data.list;
                    this.downlone = this.data.thesum;
                }
            })
        }
    }

    segmentChanged(e) {
        console.log(e.value)
        switch (e.value) {
            case 'one':
                this.PostOne();
                break;
        
            case 'two':
                this.PostTwo();
                break;
        }
    };

}
